#include <SimpleDHT.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

const char* ssid = "ShenLanJieMa";
const char* password = "sl666888";
const char* mqtt_server = "39.106.80.95";
const char* client_id = "clientId-ApjJZcy9Dh";

//房间id
const char* room_id = "KclH3335";
//用户id
const char* user_id = "dEay222J";
//消息主题
const char* TOPIC = "dEay222J_KclH3335";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;

//
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);


void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
    }
    Serial.println();
}

void reconnect() {
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect(client_id)) {
            Serial.println("connected");
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}

void setup_wifi() {
    delay(10);
    // 板子通电后要启动，稍微等待一下让板子点亮
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    
    WiFi.begin(ssid, password);
    
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}
///life -------------------------------------------------------------
void setup() {
    Serial.begin(115200);
    Serial.println("system setup");
    setup_wifi();
    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);
}

void loop() {
    
    if (!client.connected()) {
        reconnect();
    }
    client.loop();
    
    // start working...
    Serial.println("=================================");
    Serial.println("Sample DHT11...");
    
    // read without samples.
    byte temperature = 0;
    byte humidity = 0;
    int err = SimpleDHTErrSuccess;
    if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
        Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
        return;
    }
    
    Serial.print("Sample OK: ");
    Serial.print((int)temperature); Serial.print(" *C, ");
    Serial.print((int)humidity); Serial.println(" H");
    
    
    
    String payload = "{";
    payload += "\"room_id\":"; payload += "\""; payload += room_id; payload += "\""; payload += ",";
    payload += "\"temp\":"; payload += "\""; payload += temperature; payload += "\""; payload += ",";
    payload += "\"humidity\":"; payload += "\""; payload += humidity; payload += "\"";
    payload += "}";
    
    // Send payload
    char attributes[100];
    payload.toCharArray( attributes, 100 );
    client.publish(TOPIC, attributes );
    
    // DHT11 sampling rate is 1HZ.
    delay(1500);
}
